package demo.java.java8.Lambdas.demo1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;

public class FilterApples {

	public static void main(String[] args) {
		List<Apple> inventory = Arrays.asList(new Apple(80,"green"),
                new Apple(155, "green"),
                new Apple(120, "red"));
//		List<Apple> filterApples = filterApples(inventory, FilterApples::isGreenApple);
//		System.out.println(filterApples.size());
//		List<Apple> filterApples2 = filterApples(inventory, FilterApples::isHeavyApple);
//		System.out.println(filterApples2.size());
		
//		List<Apple> filterApples = filterApples(inventory, (Apple a) -> "green".equals(a.getColor()) );
//		System.out.println(filterApples.size());
//		filterApples=filterApples(inventory, (Apple a) -> a.getWeight()>150 );
//		System.out.println(filterApples.size());
		
//		Thread t=new Thread(() -> System.out.println("Hello world"));
//		t.start();
		inventory.sort((Apple a1,Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
		
	}
	
	
	public static boolean isGreenApple(Apple apple){
		return "green".equals(apple.getColor());
	}
	
	public static boolean isHeavyApple(Apple apple){
		return apple.getWeight()  >150;
	}
	
	public interface Predicate<T>{
		boolean test(T t);
	}
	
	static List<Apple> filterApples(List<Apple> inventory,Predicate<Apple> p){
		List<Apple> result = new ArrayList<>();
		for(Apple apple: inventory){
			if(p.test(apple)){
				result.add(apple);
			}
		};
		return result;
	}
	
	
	public static class Apple {
        private int weight = 0;
        private String color = "";

        public Apple(int weight, String color){
            this.weight = weight;
            this.color = color;
        }

        public Integer getWeight() {
            return weight;
        }

        public void setWeight(Integer weight) {
            this.weight = weight;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public String toString() {
            return "Apple{" +
                   "color='" + color + '\'' +
                   ", weight=" + weight +
                   '}';
        }
    }
}
